<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use App\Http\Resources\Admins\CouponUseRecrodResource;
use App\Http\Resources\Admins\UserCouponResource;
use App\Models\UserCoupon;
use App\Models\UserCouponUseRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Coupon extends AdminController
{
    public function list(Request $request)
    {
        $elem = \App\Models\Coupon::latest();

        $data = $elem->paginate($request->input('limit',10));

        return $this->ReturnPage($data);
    }

    public function listPost(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'id'    => 'required|integer',
            'name'  => 'required',
            'value' => 'required|numeric',
            'start' => 'required|date',
            'expire'=> 'required|integer',
            'type'  => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        \App\Models\Coupon::updateOrCreate(['id'=>$post['id']],$post);

        return $this->ReturnSuccessInfo();
    }

    public function record(Request $request)
    {
        $elem = UserCoupon::latest();

        $data = $elem->paginate($request->input('limit',10));

        return $this->ReturnPage(UserCouponResource::collection($data));
    }

    public function recordPost(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'mobile'    => 'present',
            'coupon_id' => 'required|integer|exists:coupons,id',
            'type'      => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $coupon = \App\Models\Coupon::find($post['coupon_id']);

        switch ($post['type']){

            case 1:

                if ($request->filled('mobile') || ! $user = \App\Models\User::firstWhere('mobile',$post['mobile'])){

                    return $this->ReturnErrorInfo(4);
                }

                UserCoupon::create([
                    'user_id'   => $user->id,
                    'coupon_id' => $coupon->id,
                    'value'     => $coupon->value,
                    'deadline'  => date('Y-m-d',strtotime('+'.$coupon->expire.' day')),
                ]);
                break;

            case 2:

                $user = \App\Models\User::where('openid','!=','')->get();

                collect($user)->each(function ($item) use ($coupon){

                    UserCoupon::create([
                        'user_id'   => $item->id,
                        'coupon_id' => $coupon->id,
                        'value'     => $coupon->value,
                        'deadline'  => date('Y-m-d',strtotime('+'.$coupon->expire.' day')),
                    ]);
                });

                break;
        }



        return $this->ReturnSuccessInfo();
    }

    public function use(Request $request)
    {
        $elem = UserCouponUseRecord::latest();

        $data = $elem->paginate($request->input('limit',10));

        return $this->ReturnPage(CouponUseRecrodResource::collection($data));
    }
}
